using System;
using System.IO;
using System.Text;
using System.Web.Hosting;

namespace CodeBackpack.Test.Http
{
    /// <summary>
    /// A worker request stub that lets you fake out the HTTP request methods.
    /// </summary>
    /// <remarks>
    /// Only few strategic methods were overriden.
    /// </remarks>
    internal class WorkerRequestStub : SimpleWorkerRequest
    {
        private readonly String requestMethod;
        private readonly String requestBody;
        private readonly Encoding requestEncoding;
        private byte[] entityBody;

        public WorkerRequestStub(String requestMethod, String requestBody, Encoding requestEncoding, String page, String query, StringWriter output)
            : base("/it/does/not/matter", @"I:\t\does\not\matter", page, query, output)
        {
            this.requestMethod = requestMethod;
            this.requestBody = requestBody;
            this.requestEncoding = requestEncoding;
        }

        public override String GetHttpVerbName()
        {
            return requestMethod;
        }

        public override bool IsEntireEntityBodyIsPreloaded()
        {
            return true;
        }

        public override byte[] GetPreloadedEntityBody()
        {
            if (entityBody == null)
            {
                if (requestBody == null)
                {
                    throw new ArgumentNullException("requestBody",
                        "You must supply non-null 'requestBody' argument!");
                }
                if (requestEncoding == null)
                {
                    throw new ArgumentNullException("requestEncoding",
                        "You must supply non-null 'requestEncoding' argument!");
                }
                entityBody = requestEncoding.GetBytes(requestBody);
            }
            return entityBody;
        }
    }
}